/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.netcracker.test.nine;
import java.util.Iterator;
import java.util.ListIterator;


/**
 *
 * @author I
 */ 
public class MyList implements Iterable<Double> {
	 
    private Double array[];
    public MyList(Double [] arr) {
        array=arr;
    }
            
	 
    @Override
    public Iterator<Double> iterator() {
        return new MyIterator();
    }

    private class MyIterator implements ListIterator<Double> 
    {

        private int current = -1;

        @Override
        public boolean hasNext() {
            return current + 1 < array.length;
        }

        @Override
        public Double next() {
            return array[++current];
        }

        @Override
        public boolean hasPrevious() {
            return array.length != 0 && current - 1 > -1;
        }

        @Override
        public Double previous() {
            return array[--current];
        }

        @Override
        public int nextIndex() {
            return current + 1;
        }

        @Override
        public int previousIndex() {
            return current - 1;
        }

        @Override
        public void remove() {
            Double old[] = array;
            array = new Double[old.length - 1];
            System.arraycopy(old, 0, array, 0, current);
            System.arraycopy(old, current + 1, array, current, array.length - current);
            --current;
        }

        @Override
        public void set(Double element) {
            array[current] = element;
        }

        @Override
        public void add(Double element) {
            int position = current == -1 ? 0 : current;
            Double old[] = array;
            array = new Double[old.length + 1];
            System.arraycopy(old, 0, array, 0, position);
            System.arraycopy(old, position, array, position + 1, old.length - position);
            array[position] = element;
        }        
    }
}




